<?php

class Bizprofile_Widgets_Menu_Widget extends Tomato_Widget
{
    protected function _prepareShow()
    {
        $enterprise_id = 0;
        $user_id = 0;
        $auth = Zend_Auth::getInstance();
        $enterprise = null;
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            if($user->enterprise){
                $enterprise_id = $user->enterprise->id;
                $user_id = $user->user_id;
                $enterprise = $user->enterprise;
            }
        }

        $params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
        $active = 'profile';
        if($params['module'] == 'bizprofile' && $params['controller'] == 'Message'){
            $active = 'message';
        }
        elseif($params['module'] == 'bizprofile' && $params['controller'] == 'Partner'){
            $active = 'partner';
        }
        elseif($params['module'] == 'b2b' && $params['controller'] == 'Blog'){
            $active = 'blog';
        }
        elseif($params['module'] == 'b2b' && $params['controller'] == 'Gallery'){
            $active = 'gallery';
        }
        elseif($params['module'] == 'shop' && $params['controller'] == 'Product'){
            $active = 'product';
        }
        $this->_view->assign('active', $active);

        /**if($params['module'] == 'bizprofile' && isset($params['enterprise_id'])){*/
        if(($params['module'] == 'bizprofile' || $params['module'] == 'b2b' || $params['module'] == 'shop') && isset($params['enterprise_id'])){
            $enterprise_id = $params['enterprise_id'];
            $enterpriseDao  = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
            $enterpriseDao->setDbConnection($conn);
            $enterprise = $enterpriseDao->getById($enterprise_id);
        }

        $user_entity = -1;
        if($enterprise_id > 0 && $enterprise){
            $this->_view->assign('enterprise', $enterprise);
            $permissionDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPermissionDao();
            $permissionDao->setDbConnection($conn);
            //$user_entity = $permissionDao->getUserEntity($user_id,$enterprise_id);
        	/** Khanh
	        * Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
	        * $user_entity = 1 : User chua login
	        * $user_entity = 2 : User da login, nhung ko la DN
	        * $user_entity = 3 : User da login, la DN khach
	        * $user_entity = 4 : User da login, la DN doi tac
	        **/
			
            if(isset($params['enterprise_id']))
			{
				//$auth = Zend_Auth::getInstance();
		        if ($auth->hasIdentity()) 
		        { 
		            $user = $auth->getIdentity();
		            if(!isset($user->enterprise->id)){
			        	$user_entity = 2; // user da login, nhung ko phai la DN
			        }
			        else
			        {						
						$partnerDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPartnerDao();
						$partnerDao->setDbConnection($conn);
		
						$pageIndex     = 1;
						$perPage = 5;
						$offset     = ($pageIndex - 1) * $perPage;
						$exp = array(
							'relationid' 	 => $params['enterprise_id'],
							'partner_type' 	 => 'buddy',
							'partner_status' => 'yes',
							'deleted'		 => 0
						);						
						//* In ralationship
						$partners = $partnerDao->find($offset, $perPage, $exp);
						$numPartners   = $partnerDao->count($exp);

						if($user->enterprise->id == $enterprise_id) $user_entity = 0;
						else{							
							if($numPartners>0){
								$user_entity = 3;// DN khach
								foreach($partners as $row){
									$expRecomment = array(
										'relationid' => $row->relationid,
										'enterprise_id' => $row->enterprise_id, // DN doi tac
									);
									if($user->enterprise->id == $row->enterprise_id)
									{
										$user_entity = 4; // DN doi tac
									}
								}
							}
						}
			        }
		        }
		        else 
		        {
		        	$user_entity = 1; // user chua login 
		        }
			}
			
			/**
		`	* END : Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
		    **/
			else 
			{
				$user_entity = $permissionDao->getUserEntity($user_id,$enterprise_id);
			}
			
        	if ($auth->hasIdentity()) { 
	            $user = $auth->getIdentity();
				if($user->enterprise){
					if($user->enterprise->id == $enterprise_id) // user đã login thuộc DN đang xem 
			    	{
			    		$permission_viewall = 1;
			    		$this->_view->assign('permission_viewall', $permission_viewall);
			    	}
				}
        	}
            $this->_view->assign('user_entity', $user_entity);

            $permissionDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPermissionDao();
            $permissionDao->setDbConnection($conn);
            $permission = $permissionDao->getPermissionByUserEntity($enterprise_id, $user_entity);
            
            if($permission != null){
                $permission->enterprise_info = unserialize($permission->enterprise_info);
                $this->_view->assign('permission', $permission);
            }
        }

        $countMessage 		  = 0;
        $countPartner 		  = 0;
		$countRecomment		  = 0;
        if($user_entity == 0){
            $messageDetailDao  = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getMessageDetailDao();
            $messageDetailDao->setDbConnection($conn);
            $exp = array(
				'enterprise_id' => $user->enterprise->id,
				'parentmessageid' => 1,
                'messageread' => 0
            );
            $countMessage = $messageDetailDao->count($exp);
			
            $partnerDao  = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPartnerDao();
            $partnerDao->setDbConnection($conn);
            $exp = array(
				'enterprise_id'  => $user->enterprise->id,
				'notify' 		 => 'unview',
                'partner_status' => 'no'				
            );
            $countPartner = $partnerDao->count($exp);
            $exp = array(				  
				'relationid'  => $user->enterprise->id,
				'notify' 		 => 'unview',
                'partner_status' => 'yes',
				'recomment'		 => true	
            );
            $countRecomment = $partnerDao->count($exp);					
        }
        $countPartnerAll   =  $countPartner + $countRecomment + $countMessage;
		$this->_view->assign('count_unread_message', $countMessage);
        $this->_view->assign('count_unread_partner', $countPartner);		
		$this->_view->assign('count_unread_recomment',$countRecomment);
		$this->_view->assign('count_unread_all',$countPartnerAll);		
    }

    protected function _prepareConfig()
    {

    }
}
